// 连接数据库
module.exports = function (success, error) {
    if(typeof error !=='function'){
        error=()=>{
            console.log('数据库连接失败');
        }
    }
    // 导入mongoose
    const mongoose = require('mongoose')
    // 导入配置文件
    const { DBHOST, DBPORT, DBNAME } = require('../config/config')
    // 连接mongodb服务器
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`,{ useNewUrlParser: true, useUnifiedTopology: true })
    // 设置回调
    // 连接成功的回调
    mongoose.connection.once('open', () => {
        success()
    })
    // 连接失败的回调
    mongoose.connection.on('error', () => {
        error()
    })
    // 连接断开的回调
    mongoose.connection.on('close',()=>{
        console.log('连接关闭')
    })
}